package com.yonyou.pmclouds.projectlog;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogVo;
import com.yonyou.pmclouds.projectlog.rmiitf.ProjectLogQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 智能日志--移动端控制器
 * @author xiejianyu
 * 2019-09-17
 */
@Slf4j
@RestController
@RequestMapping("/mobile/projectLog")
public class ProjectLogMobController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProjectLogQuery query;
    @Autowired
    private UserInfoQuery userQuery;

    private static final String[] USER_ID_FIELDS = {ProjectLogVo.CREATOR};
    private static final String[] USER_NAME_FIELDS = {ProjectLogVo.CREATOR_NAME};

    @RequestMapping("/query")
    public ProjectLogVo query(@RequestParam("pkTenant") String pkTenant, @RequestParam(value = "pkProject") String pkProject, @RequestParam("logDate") String logDate, HttpServletRequest request) throws BusinessException {
        try {
            if (StringUtils.isEmpty(pkProject)) {
                throw new BusinessException("缺少项目信息");
            }

            if (StringUtils.isEmpty(pkTenant)) {
                throw new BusinessException("缺少租户信息");
            } else {
                RuntimeEnvironment.setTenantId(pkTenant);
            }

            Date date = null;
            Calendar calendar = Calendar.getInstance();
            if (StringUtils.isEmpty(logDate)) {
                calendar.setTime(new Date());
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                date = calendar.getTime();
            } else {
                SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
                date = sdf.parse(logDate);
                if (date.after(new Date())) {
                    throw new BusinessException("日期不能超过今天");
                }
            }

            ProjectLogVo projectLogVo = query.queryByProjectAndDate(pkProject, date);
            userQuery.tranUserInfoField(new ProjectLogVo[]{projectLogVo}, USER_ID_FIELDS, USER_NAME_FIELDS);
            return projectLogVo;
        } catch (Exception e) {
            if (e instanceof ParseException) {
                throw new BusinessException("日期不合法");
            } else if (e instanceof BusinessException) {
                throw (BusinessException)e;
            } else {
                log.error("移动端查询智能日志出错", e);
                throw new BusinessException("智能日志查询出错");
            }
        }
    }
}
